import { Body, Controller, Delete, Get, Param, ParseIntPipe, Patch, Post, Query } from '@nestjs/common'
import { ApiOperation, ApiParam, ApiResponse, ApiTags } from '@nestjs/swagger'
import { ApiSecurityAuth } from '~/common/decorators/swagger.decorator'
import { definePermission, Perm } from '~/modules/admin/auth/decorators/permission.decorator'

import { CreateAppDto } from '../dto/create-app.dto'
import { QueryAppDto } from '../dto/query-app.dto'
import { UpdateAppDto } from '../dto/update-app.dto'
import { UpdateSortDto } from '../dto/update-sort.dto'
import { AppService } from '../services/app.service'

export const permissions = definePermission('setting:app', {
  LIST: 'list',
  CREATE: 'create',
  READ: 'read',
  UPDATE: 'update',
  DELETE: 'delete',
} as const)

@ApiTags('Setting - 首页应用管理')
@ApiSecurityAuth()
@Controller('setting/app')
export class AppController {
  constructor(private readonly appService: AppService) { }

  @ApiOperation({ summary: '创建首页应用' })
  @ApiResponse({ status: 201, description: '创建成功' })
  @Perm(permissions.CREATE)
  @Post()
  create(@Body() createAppDto: CreateAppDto) {
    return this.appService.create(createAppDto)
  }

  @ApiOperation({ summary: '获取首页应用列表' })
  @ApiResponse({ status: 200, description: '获取成功' })
  @Perm(permissions.LIST)
  @Get()
  findAll(@Query() query: QueryAppDto) {
    return this.appService.findAll(query)
  }

  @ApiOperation({ summary: '获取单个首页应用' })
  @ApiParam({ name: 'id', description: '应用ID' })
  @ApiResponse({ status: 200, description: '获取成功' })
  @Perm(permissions.READ)
  @Get(':id')
  findOne(@Param('id', ParseIntPipe) id: number) {
    return this.appService.findOne(+id)
  }

  @ApiOperation({ summary: '更新首页应用信息' })
  @ApiParam({ name: 'id', description: '应用ID' })
  @ApiResponse({ status: 200, description: '更新成功' })
  @Perm(permissions.UPDATE)
  @Patch(':id')
  update(@Param('id', ParseIntPipe) id: number, @Body() updateAppDto: UpdateAppDto) {
    return this.appService.update(+id, updateAppDto)
  }

  @ApiOperation({ summary: '删除首页应用' })
  @ApiParam({ name: 'id', description: '应用ID' })
  @ApiResponse({ status: 200, description: '删除成功' })
  @Perm(permissions.DELETE)
  @Delete(':id')
  remove(@Param('id', ParseIntPipe) id: number) {
    return this.appService.remove(+id)
  }

  @ApiOperation({ summary: '根据资源类型获取标签关联的资源列表' })
  @ApiResponse({ status: 200, description: '获取成功' })
  @Perm(permissions.LIST)
  @Get('resources-tree')
  async getResourcesTree(@Query('resourceType', ParseIntPipe) resourceType: number) {
    if (resourceType === 1) {
      return this.appService.findAllVideoTagsWithMovies()
    }
    else if (resourceType === 2) {
      return this.appService.findAllBookTagsWithBooks()
    }
    else {
      return []
    }
  }

  @ApiOperation({ summary: '批量更新排序' })
  @ApiResponse({ status: 200, description: '更新成功' })
  @Perm(permissions.UPDATE)
  @Post('sort')
  updateSort(@Body() updateSortDto: UpdateSortDto) {
    return this.appService.updateSort(updateSortDto.sortList)
  }
}
